package com.cash.app.utils

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
 * Navigation bar utility for handling navigation bar related operations
 */
object NavigationBarUtils {
    /**
     * Get navigation bar height
     */
    fun getNavigationBarHeight(context: Context): Int {
        val resources = context.resources
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            resources.getDimensionPixelSize(resourceId)
        } else 0
    }

    /**
     * Set navigation bar color with full customization
     */
    fun setNavigationBarColor(window: Window, @ColorInt color: Int) {
        try {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)

            window.navigationBarColor = color

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                // Android 10 (Q) and above support navigation bar transparency
                window.isNavigationBarContrastEnforced = true
            }

            WindowCompat.setDecorFitsSystemWindows(window, false)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * Check if device has navigation bar
     */
    fun hasNavigationBar(context: Context): Boolean {
        val resources = context.resources
        val resourceId = resources.getIdentifier("config_showNavigationBar", "bool", "android")
        return if (resourceId > 0) {
            resources.getBoolean(resourceId)
        } else {
            // Check the minimum height of the navigation bar
            val navBarHeight = getNavigationBarHeight(context)
            navBarHeight > 0
        }
    }

    /**
     * Make navigation bar transparent
     */
    fun setNavigationBarTransparent(window: Window) {
        setNavigationBarColor(window, Color.TRANSPARENT)
    }

    /**
     * Hide navigation bar
     */
    fun hideNavigationBar(window: Window) {
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.hide(WindowInsetsCompat.Type.navigationBars())
        controller.systemBarsBehavior =
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }

    /**
     * Show navigation bar
     */
    fun showNavigationBar(window: Window) {
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.show(WindowInsetsCompat.Type.navigationBars())
    }
} 